package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.base.BaseSupport;
import com.mt.roll.core.dal.entity.SysDictData;
import com.mt.roll.core.dal.entity.SysDictType;
import com.mt.roll.core.dal.service.SysDictDataServiceBiz;
import com.mt.roll.core.dal.service.SysDictTypeServiceBiz;
import com.mt.roll.core.dto.LoginUser;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.PlatformDictDataParam;
import com.mt.roll.wms.params.PlatformDictDataQueryParam;
import com.mt.roll.wms.params.PlatformDictTypeParam;
import com.mt.roll.wms.params.PlatformDictTypeQueryParam;
import com.mt.roll.wms.service.PlatformDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统字典管理服务实现类
 *
 * @author yangyang.jiang
 * @create 2018-11-12 21:02
 **/
@Service
public class PlatformDictServiceImpl extends BaseSupport implements PlatformDictService {

    @Autowired
    private SysDictTypeServiceBiz sysDictTypeServiceBiz;

    @Autowired
    private SysDictDataServiceBiz sysDictDataServiceBiz;

    /**
     * 分页查询系统字典类型
     *
     * @param platformDictTypeQueryParams
     * @return
     */
    @Override
    public R<IPage> selectPlatformDictTypeListByPage(PlatformDictTypeQueryParam platformDictTypeQueryParams) {
        Page page = new Page();
        page.setCurrent(platformDictTypeQueryParams.getCurrent());
        page.setSize(platformDictTypeQueryParams.getSize());
        Wrapper wrapper = new QueryWrapper();
        ((QueryWrapper) wrapper).like(StringUtils.isNotEmpty(platformDictTypeQueryParams.getTypeName()), SysDictType.TYPE_NAME, platformDictTypeQueryParams.getTypeName());
        ((QueryWrapper) wrapper).like(StringUtils.isNotEmpty(platformDictTypeQueryParams.getTypeCode()), SysDictType.TYPE_CODE, platformDictTypeQueryParams.getTypeCode());
        ((QueryWrapper) wrapper).orderByDesc(SysDictType.CREATE_TIME);
        IPage iPage = sysDictTypeServiceBiz.page(page, wrapper);
        return R.ok(iPage);
    }

    /**
     * 保存字典类型信息
     *
     * @param platformDictTypeParams
     * @return
     */
    @Override
    @Transactional
    public R<Object> savePlatformDictType(PlatformDictTypeParam platformDictTypeParams, LoginUser loginUser) {
        //判断类型编码是否重复
        List<SysDictType> list=sysDictTypeServiceBiz.list(new QueryWrapper<SysDictType>().eq(SysDictType.TYPE_CODE,platformDictTypeParams.getTypeCode()));
        if (list.size()>0){
            return R.failed("字典类型编码已存在,请勿重复添加");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(platformDictTypeParams, sysDictType);
        sysDictType.setCreator(loginUser.getUserId());
        sysDictType.setCreatorName(loginUser.getUserName());
        sysDictType.setCreateTime(LocalDateTime.now());
        sysDictTypeServiceBiz.save(sysDictType);
        return R.ok("保存成功");
    }

    /**
     * 根据id查询字典类型
     *
     * @param dictTypeId
     * @return
     */
    @Override
    public R<SysDictType> selectDictTypeById(Long dictTypeId) {
        SysDictType sysDictType = sysDictTypeServiceBiz.getById(dictTypeId);
        return R.ok(sysDictType);
    }

    /**
     * 修改字典类型
     *
     * @param platformDictTypeParams
     * @return
     */
    @Override
    @Transactional
    public R<Object> updatePlatformDictType(PlatformDictTypeParam platformDictTypeParams, LoginUser loginUser) {
        List<SysDictType> list=sysDictTypeServiceBiz.list(new QueryWrapper<SysDictType>().ne(SysDictType.ID,platformDictTypeParams.getId()).eq(SysDictType.TYPE_CODE,platformDictTypeParams.getTypeCode()));
        if (list.size()>0){
            return R.failed("字典类型编码已存在,请勿修改重复");
        }
        SysDictType sysDictType = new SysDictType();
        BeanUtils.copyProperties(platformDictTypeParams, sysDictType);
        sysDictType.setCreator(loginUser.getUserId());
        sysDictType.setUpdaterName(loginUser.getUserName());
        sysDictType.setUpdateTime(LocalDateTime.now());
        sysDictTypeServiceBiz.updateById(sysDictType);
        List<SysDictData> sysDictDatas = sysDictDataServiceBiz.list(new QueryWrapper<SysDictData>().eq("DICT_TYPE_ID", platformDictTypeParams.getId()));
        if(sysDictDatas.size()>0){
            for(SysDictData sysDictData : sysDictDatas){
                sysDictData.setTypeName(platformDictTypeParams.getTypeName());
                sysDictData.setTypeCode(platformDictTypeParams.getTypeCode());
                sysDictDataServiceBiz.updateById(sysDictData);
            }
        }
        return R.ok("修改成功");
    }

    /**
     * 删除字典类型
     *
     * @param dictTypeId
     * @return
     */
    @Override
    @Transactional
    public R<Object> deletePlatformDictType(Long dictTypeId) {
        List<SysDictData> list = sysDictDataServiceBiz.list(new QueryWrapper<SysDictData>().eq(SysDictData.DICT_TYPE_ID,dictTypeId));
        if (list.size() > 0) {
            return R.failed("该类型存在字典值，请先删除子项");
        }
        sysDictTypeServiceBiz.removeById(dictTypeId);
        return R.ok("删除成功");
    }

    /**
     * 分页查询字典
     *
     * @param platformDictDataQueryParams
     * @return
     */
    @Override
    public R<IPage> selectPlatformDictDataListByPage(PlatformDictDataQueryParam platformDictDataQueryParams) {
        Page page = new Page();
        page.setCurrent(platformDictDataQueryParams.getCurrent());
        page.setSize(platformDictDataQueryParams.getSize());
        Wrapper wrapper = new QueryWrapper();
        ((QueryWrapper) wrapper).like(StringUtils.isNotEmpty(platformDictDataQueryParams.getDataCode()), SysDictData.DATA_CODE, platformDictDataQueryParams.getDataCode());
        ((QueryWrapper) wrapper).like(StringUtils.isNotEmpty(platformDictDataQueryParams.getDataName()), SysDictData.DATA_NAME, platformDictDataQueryParams.getDataName());
        ((QueryWrapper) wrapper).eq(SysDictData.DICT_TYPE_ID, platformDictDataQueryParams.getDictTypeId());
        ((QueryWrapper) wrapper).orderByAsc(SysDictData.CREATE_TIME);
        IPage iPage = sysDictDataServiceBiz.page(page, wrapper);
        return R.ok(iPage);
    }

    /**
     * 保存数据字典
     *
     * @param platformDictDataParams
     * @return
     */
    @Override
    @Transactional
    public R<Object> savePlatformDictData(PlatformDictDataParam platformDictDataParams, LoginUser loginUser) {
        SysDictType sysDictType = sysDictTypeServiceBiz.getById(platformDictDataParams.getDictTypeId());
        if(sysDictType==null){
            return R.failed("类型编码不存在");
        }
        List<SysDictData> list=sysDictDataServiceBiz.list(new QueryWrapper<SysDictData>().eq(SysDictData.DICT_TYPE_ID,platformDictDataParams.getDictTypeId()).eq(SysDictData.DATA_CODE,platformDictDataParams.getDataCode()));
        if (list.size()>0){
            return R.failed("字典编码已存在,请勿重复添加");
        }
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(platformDictDataParams, sysDictData);
        sysDictData.setTypeCode(sysDictType.getTypeCode());
        sysDictData.setTypeName(sysDictType.getTypeName());
        sysDictData.setCreateTime(LocalDateTime.now());
        sysDictData.setCreator(loginUser.getUserId());
        sysDictData.setCreatorName(loginUser.getUserName());
        sysDictDataServiceBiz.save(sysDictData);
        return R.ok("保存成功");
    }

    /**
     * 修改数据字典
     *
     * @param platformDictDataParams
     * @param loginUser
     * @return
     */
    @Override
    @Transactional
    public R<Object> updatePlatformDictData(PlatformDictDataParam platformDictDataParams, LoginUser loginUser) {
        SysDictType sysDictType = sysDictTypeServiceBiz.getById(platformDictDataParams.getDictTypeId());
        if(sysDictType==null){
            return R.failed("类型编码不存在");
        }
        List<SysDictData> list=sysDictDataServiceBiz.list(new QueryWrapper<SysDictData>().ne(SysDictData.ID,platformDictDataParams.getId()).eq(SysDictData.DICT_TYPE_ID,platformDictDataParams.getDictTypeId()).eq(SysDictData.DATA_CODE,platformDictDataParams.getDataCode()));
        if (list.size()>0){
            return R.failed("字典编码已存在,请勿修改重复");
        }
        SysDictData sysDictData = new SysDictData();
        BeanUtils.copyProperties(platformDictDataParams, sysDictData);
        sysDictData.setUpdater(loginUser.getUserId());
        sysDictData.setUpdaterName(loginUser.getUserName());
        sysDictData.setUpdateTime(LocalDateTime.now());
        sysDictDataServiceBiz.updateById(sysDictData);
        return R.ok("修改成功");

    }

    /**
     * 根据id查询数据字典
     *
     * @param dictId
     * @return
     */
    @Override
    public R<SysDictData> selectPlatformDictDataById(Long dictId) {
        SysDictData sysDictData = sysDictDataServiceBiz.getById(dictId);
        return R.ok(sysDictData);
    }

    /**
     * 删除数据字典
     *
     * @param dictId
     * @return
     */
    @Override
    public R<Object> deletePlatformDictData(Long dictId) {
        sysDictDataServiceBiz.removeById(dictId);
        return R.ok("删除成功");
    }

    /**
     * 查询字典类型列表
     *
     * @return
     */
    @Override
    public R<List<SysDictType>> selectDictTypeByList() {
        QueryWrapper<SysDictType> qr = new QueryWrapper();
        qr.select(SysDictType.TYPE_CODE, SysDictType.TYPE_NAME);
        List<SysDictType> list = sysDictTypeServiceBiz.list(qr);
        return R.ok(list);
    }
}
